<?php
require_once 'database21.php';

class Student extends Database {
    public function createStudent($name, $gender, $birth_date, $contact_info, $class_id) {
        try {
            $stmt = $this->conn->prepare("INSERT INTO students (student_name, gender, birth_date, contact_info, class_id) VALUES (:name, :gender, :birth_date, :contact_info, :class_id)");
            $stmt->execute([
                ':name' => $name,
                ':gender' => $gender,
                ':birth_date' => $birth_date,
                ':contact_info' => $contact_info,
                ':class_id' => $class_id
            ]);
            return $this->conn->lastInsertId();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function getStudents() {
        try {
            $stmt = $this->conn->prepare("
                SELECT s.*, c.class_name 
                FROM students s 
                LEFT JOIN classes c ON s.class_id = c.class_id
            ");
            $stmt->execute();
            return $stmt->fetchAll();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function getStudent($id) {
        try {
            $stmt = $this->conn->prepare("
                SELECT s.*, c.class_name 
                FROM students s 
                LEFT JOIN classes c ON s.class_id = c.class_id 
                WHERE s.student_id = :id
            ");
            $stmt->execute([':id' => $id]);
            return $stmt->fetch();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function updateStudent($id, $name, $gender, $birth_date, $contact_info, $class_id) {
        try {
            $stmt = $this->conn->prepare("UPDATE students SET student_name = :name, gender = :gender, birth_date = :birth_date, contact_info = :contact_info, class_id = :class_id WHERE student_id = :id");
            $stmt->execute([
                ':id' => $id,
                ':name' => $name,
                ':gender' => $gender,
                ':birth_date' => $birth_date,
                ':contact_info' => $contact_info,
                ':class_id' => $class_id
            ]);
            return $stmt->rowCount();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }

    public function deleteStudent($id) {
        try {
            // 检查是否有成绩记录关联到该学生
            $stmt = $this->conn->prepare("SELECT * FROM scores WHERE student_id = :id");
            $stmt->execute([':id' => $id]);

            if ($stmt->rowCount() > 0) {
                throw new Exception("无法删除, 该学生有成绩记录存在");
            }

            $stmt = $this->conn->prepare("DELETE FROM students WHERE student_id = :id");
            $stmt->execute([':id' => $id]);
            return $stmt->rowCount();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        } catch (Exception $e) {
            die("错误: " . $e->getMessage());
        }
    }

    // 获取所有班级信息，用于学生管理中的班级选择
    public function getClasses() {
        try {
            $stmt = $this->conn->prepare("SELECT * FROM classes");
            $stmt->execute();
            return $stmt->fetchAll();
        } catch (PDOException $e) {
            die("错误: " . $e->getMessage());
        }
    }
}
?>